
#include "touch_i2c_bus.h"
    
static uint8_t write_reg(void *handle, uint8_t addr, uint16_t reg, uint8_t *buff, uint8_t len)
{
    touch_i2c_bus_handle_t *t = (touch_i2c_bus_handle_t *)handle;

    t->transfer.slaveAddress = addr;
    t->transfer.subaddress = reg;
    t->transfer.subaddressSize = 2;
    t->transfer.data = buff;
    t->transfer.dataSize = len;
    t->transfer.direction = kI2C_Write;
    return i2c_simulate_transfer(&t->i2c, &t->transfer);
}
    
static uint8_t read_reg(void *handle, uint8_t addr, uint16_t reg, uint8_t *buff, uint8_t len)
{
    touch_i2c_bus_handle_t *t = (touch_i2c_bus_handle_t *)handle;

    t->transfer.slaveAddress = addr;
    t->transfer.subaddress = reg;
    t->transfer.subaddressSize = 2;
    t->transfer.data = buff;
    t->transfer.dataSize = len;
    t->transfer.direction = kI2C_Read;
    return i2c_simulate_transfer(&t->i2c, &t->transfer);
}
    
static void reset(void *handle, uint16_t ms1, uint16_t ms2)
{
    touch_i2c_bus_handle_t *t = (touch_i2c_bus_handle_t *)handle;
    hal_pin_reset(&t->rst);
    hal_delay_ms(ms1);
    hal_pin_set(&t->rst);
    hal_delay_ms(ms2);
}

touch_bus_t touch_i2c_bus = {
    .read_reg = read_reg,
    .write_reg = write_reg,
    .reset = reset,
};